package Iol;

import java.io.*;
import java.util.Arrays;
import java.util.Comparator;

/**
 * description:姚
 * author:zcx
 * lenovo
 * 时间：2022.01.20.11.11
 */
public class Io8Y {
    public static void main(String[] args) throws IOException {
        File srcFile = new File("D:\\chrome\\vs.exe");
        //拆分文件
        judge(srcFile,5);
        //合并文件
        //merge(new File("D:\\chrome\\vs.exe.0"),new File("D:\\chrome\\vs.exe.1"),new File("D:\\chrome\\vs.exe.2"),
                //new File("D:\\chrome\\vs.exe.3"),new File("D:\\chrome\\vs.exe.4"));
    }

    public static void merge(File ... file) throws IOException {
        //Pattern compile = Pattern.compile("(.*)\\.(.*)");
        for(File f:file){
            if(f.isDirectory()){
                return;
            }
            /*else{
                Matcher matcher = compile.matcher(f.getName());
                if(matcher.matches()){
                    System.out.println(matcher.group(0));
                    System.out.println(matcher.group(1));
                    System.out.println(matcher.group(2));
                }
            }*/
        }
        Arrays.sort(file, new Comparator<File>() {
            @Override
            public int compare(File o1, File o2) {
                int i=Integer.parseInt(o1.getName().substring(o1.getName().lastIndexOf(".")+1));
                int j=Integer.parseInt(o2.getName().substring(o2.getName().lastIndexOf(".")+1));
                return i-j;
            }
        });
        File file1 = new File(file[0].getParentFile(), file[0].getName().substring(0, file[0].getName().lastIndexOf(".")));
        file1.createNewFile();
        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file1));
        BufferedInputStream bis=null;
        for(File f:file){
            bis=new BufferedInputStream(new FileInputStream(f));
            byte [] bytes=bis.readAllBytes();
            bos.write(bytes,0,bytes.length);
        }
        bis.close();
        bos.close();
        for(File f:file){
            f.delete();
        }
    }

    public static void judge(File srcFile,int x) throws IOException {
        if(srcFile.exists()&&srcFile.isFile()){
            File childDir = new File(new File(srcFile.getAbsolutePath()).getParent());
            spit(srcFile,childDir,x);
        }else{
            System.out.printf("%s 不是文件无法操作！",srcFile);
        }

    }

    public static void spit(File srcFile, File childDir,int x) throws IOException {
        long length = srcFile.length();
        //默认分为3个文件
        if(x<=-1){
            x=3;
        }
        long[] childLength=new long[x];
        long cLength=length/x;
        if((cLength=length/x)==0){
            for(int i=0;i<x;i++){
                childLength[i]=cLength;
            }
        }
        else{
            for(int i=0;i<x-1;i++){
                childLength[i]=cLength;
            }
            childLength[x-1]=cLength+length%x;
        }
        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(srcFile));
        BufferedOutputStream bos=null;
        byte [] bytearr=bis.readAllBytes();
        int num=0;
        for(int i=0;i<x;i++){
            File file = new File(childDir, srcFile.getName() + "."+i);
            //if(file.createNewFile()){
            bos = new BufferedOutputStream(new FileOutputStream(file));
            bos.write(bytearr,num,(int)childLength[i]);
            num+=(int)childLength[i];
            bos.flush();
            bos.close();
        }
        bos.close();
        bis.close();
    }
}
